/*
*   Copyright: Fabian Hajek (2012)  Programmieren: C   -   Semesterarbeit WS 2011/2012
*
*   Projekt: Schreinerei.exe
*
*   Author: $Author: webmaster@die-schwarzen-schwaben.de $
*
*   Name: func_load.c
*   URL:    $URL: http://semesterarbeit-wi-11-12.googlecode.com/svn/trunk/Semesterarbeit2/Schreinerei/Funktionen/func_load.c $
*
*   Description:   Beeinhaltet die Lade-Funktione
*
*   Last update:    $Date: 2012-02-24 21:45:10 +0000 (Fri, 24 Feb 2012) $
*
*   Rev:      $Rev: 56 $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../Funktionen/func_input.h"
#include "../default/structures.h"
#include "../Definitionen/define.h"
#include "func_load.h"

void load()
{
    int z;
    int i;
    char buf[30];
    char name[30];
    int ressources_quantity;
    int steps_quantity;
    float price;
    char entity;
    float time;
    unsigned int stock;
    FILE *pF;
    pF = fopen("temp.sv","r");
    if(pF)
    {
        /*Bestehenden Speicherplatz freigeben*/             /*SPEICHERPLATZ WIRD NICHT FREIGEGEBEN*/
        while(p_ressource_end)
        {
            p_ressource = p_ressource_end;
            p_ressource_end = p_ressource_end->prev_ressource;
            free(p_ressource);
        }

        while(p_schrank_end)
        {
            p_schrank = p_schrank_end;
            p_schrank_end = p_schrank_end->prev_schrank;
            free(p_schrank);
        }

        while(p_step_end)
        {
            p_step = p_step;
            p_step_end = p_step_end->prev_step;
            free(p_step);
        }


        /*START LOAD RESSOURCES*/
        fgets(buf,30,pF);
        if(!strcmp(buf,"[START RESSOURCES]\n"))
        {
            p_ressource = 0;        /*Es gibt keine vorherige Ressource*/
            while(fgets(buf,30,pF) && strcmp(buf,"[END RESSOURCES]\n"))
            {
                    buf[strlen(buf)-1] = 0;                         /*Newline entfernen*/
                    strcpy(name,buf);                               /*Name*/
                    fgets(buf,30,pF);                               /*Preis*/
                    sscanf(buf,"%f",&price);
                    fgets(buf,30,pF);                               /*Einheit*/
                    sscanf(buf,"%c",&entity);
                    fgets(buf,30,pF);                               /*Lager*/
                    sscanf(buf,"%i",&stock);

                    /*Erstelle Ressource*/
                    p_ressource_end = (RESSOURCE *)malloc(sizeof(RESSOURCE));
                    strcpy(p_ressource_end->name,name);
                    p_ressource_end->price = price;
                    p_ressource_end->entity = entity;
                    p_ressource_end->stock = stock;

                    if(p_ressource)
                    {
                        p_ressource->next_ressource = p_ressource_end;
                    }

                    /*Pointer setzen*/
                    p_ressource_end->prev_ressource = p_ressource;
                    p_ressource = p_ressource_end;
            }
        }
        printf("Ressourcen erfolgreich geladen\n");
        /*END LOAD RESSOURCES*/

        /*START LOAD STEPS*/
        fgets(buf,30,pF);
        if(!strcmp(buf,"[START STEPS]\n"))
        {
            p_step = 0;
            while(fgets(buf,30,pF) && strcmp(buf,"[END STEPS]\n"))          /*Gehe nur bis [END STEPS]*/
            {
                    buf[strlen(buf)-1] = 0;                                 /*Newline entfernen*/

                    /*Erstelle STEP*/
                    p_step_end = (STEP *)malloc(sizeof(STEP));
                    strcpy(p_step_end->name,buf);

                    i = 1;
                    if(strcmp(fgets(buf,30,pF),"\n"))    /*Pointer vorhanden*/       /*UNTERSCHEIDUNG ZWISCHEN POINTER AUF RESSOURCES UND NAME, DA DEFAULT WERTE OHNE POINTER EINGETRAGEN WERDEN*/
                    {
                        buf[strlen(buf)-1] = 0;
                        p_ressource = p_ressource_end;
                        while(p_ressource)
                        {
                            if(!strcmp(p_ressource->name,buf))
                            {
                                p_step_end->p_ressources = p_ressource;     /*Pointer Wert eintragen*/
                                break;
                            }
                            p_ressource=p_ressource->prev_ressource;
                        }
                        i = 0;                  /*Signal um nicht erneut zu suchen*/
                    }
                    fgets(buf,30,pF);                                       /*Zeit*/
                    sscanf(buf,"%f",&time);
                    p_step_end->time = time;

                    fgets(buf,30,pF);                                       /*Preis*/
                    sscanf(buf,"%f",&price);
                    p_step_end->price = price;

                    fgets(buf,30,pF);

                    if(i)           /*Falls kein Pointer vorhanden*/                                                /*Kein Pointer vorhanden*/
                    {
                        buf[strlen(buf)-1] = 0;
                        p_ressource = p_ressource_end;
                        while(p_ressource)
                        {
                            if(!strcmp(p_ressource->name,buf))
                            {
                                i = 0;
                                p_step_end->p_ressources = p_ressource;     /*Pointer Wert eintragen*/
                                break;
                            }
                            p_ressource = p_ressource->prev_ressource;
                        }
                    }

                    if(i)       /*Keine Ressource fuer STEP benoetigt*/
                    {

                       p_step_end->p_ressources = NULL;
                       strcpy(p_step_end->ressource,"");  /*Kein Ressource Namen*/
                    }

                    if(p_step)
                    {
                        p_step->next_step = p_step_end;
                    }

                    /*Pointer setzen*/
                    p_step_end->prev_step = p_step;
                    p_step = p_step_end;
            }
        }
        printf("Arbeitsschritte erfolgreich geladen\n");
        /*END LOAD STEPS*/

        /*START LOAD SCHRAENKE*/
        fgets(buf,30,pF);
        if(!strcmp(buf,"[START SCHRAENKE]\n"))
        {
            p_schrank = 0;
            fgets(buf,30,pF);
            while(strcmp(buf,"[END SCHRAENKE]\n"))      /*Gehe nur bis [END SCHRAENKE]*/
            {
                    buf[strlen(buf)-1] = 0;                                 /*Newline entfernen*/


                    /*Erstelle Schrank*/
                    p_schrank_end = (SCHRANK *)malloc(sizeof(SCHRANK));
                    strcpy(p_schrank_end->name,buf);


                    /*STEPS auslesen*/
                    z = 0;
                    while(fgets(buf,30,pF) && !strcmp(buf,"[STEP]\n"))  /*Nur wenn STEP folgt*/
                    {
                        fgets(buf,30,pF);
                        buf[strlen(buf)-1] = 0;

                        /*STEP finden um Adresse zu erhalten*/
                        p_step = p_step_end;

                        strcpy(name,buf);
                        while(p_step && !strcmp(buf,name))
                        {
                            if(!strcmp(p_step->name,buf))                      /*Gefunden*/
                            {
                                fgets(buf,30,pF);                                   /*Anzahl und in naechste Zeile springen*/
                                sscanf(buf,"%i",&steps_quantity);
                                p_schrank_end->p_steps[z] = p_step;       /*Zeiger eintragen*/
                                p_schrank_end->steps_quantity[z] = steps_quantity;  /*Anzahl eintragen*/
                                z++;
                            }
                            p_step = p_step->prev_step;
                        }
                    }

                    /*Restliche Step-Pointer auf 0 setzen*/
                    for(i=z;i<MAX_SCHRANK_RESOURCE;i++)
                    {
                        p_schrank_end->p_steps[i] = 0;
                    }



                    /*Ressourcen auslesen*/
                    z = 0;
                    while(!strcmp(buf,"[RESSOURCE]\n"))  /*Nur wenn RESSOURCE folgt*/
                    {
                        fgets(buf,30,pF);
                        buf[strlen(buf)-1] = 0;

                        /*Resource finden um Adresse zu erhalten*/
                        p_ressource = p_ressource_end;

                        strcpy(name,buf);
                        while(p_ressource && !strcmp(buf,name))
                        {
                            if(!strcmp(p_ressource->name,buf))                      /*Gefunden*/
                            {
                                fgets(buf,30,pF);                                   /*Anzahl und in naechste Zeile springen*/
                                sscanf(buf,"%i",&ressources_quantity);
                                p_schrank_end->p_ressources[z] = p_ressource;       /*Zeiger eintragen*/
                                p_schrank_end->ressources_quantity[z] = ressources_quantity;  /*Anzahl eintragen*/
                                z++;
                            }
                            p_ressource = p_ressource->prev_ressource;
                        }
                        fgets(buf,30,pF);
                    }

                    /*Restliche Ressource-Pointer auf 0 setzen UND restliche RESSOURCE-QUANTITY == 0 -> func_save*/
                    for(i=z;i<MAX_SCHRANK_RESOURCE;i++)
                    {
                        p_schrank_end->p_ressources[i] = 0;
                        p_schrank_end->ressources_quantity[i] = 0;
                    }

                    if(p_schrank)
                    {
                        p_schrank->next_schrank = p_schrank_end;
                    }

                    /*Pointer setzen*/
                    p_schrank_end->prev_schrank = p_schrank;
                    p_schrank = p_schrank_end;
            }
        }
        printf("Schr\x84nke erfolgreich geladen\n");
        /*END LOAD SCHRAENKE*/
        log("Speicherst\x84nde wurden erfolgreich geladen",0,1);
    }
    else
    {
        printf("\nKonnte Datei temp.sv nicht \x94 \bffnen\n");
    }
}
